package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.ArrangeDiaryService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.*;

/**
 * 随访日记功能
 */
@RestController
@RequestMapping("/arrangeDiary")
public class ArrangeDiaryController {

    @Autowired
    private JedisPool jedisPool;
    @Reference
    private ArrangeDiaryService arrangeDiaryService;

    /**
     * 修改随访日记信息
     * @param map
     * @return
     */
    @RequestMapping("/editArrangeDiary")
    public Result editArrangeDiary(@RequestBody Map<String,Object> map) {
        try {
            Map formData = (Map) map.get("formData");
            List list = (List) map.get("fileList");
            Map<String,Object> fileList = (Map<String, Object>) list.get(0);
            Result result = arrangeDiaryService.editArrangeDiary(formData, fileList);
            return result;
        } catch(Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_ARRANGE_DIARY_FAIL);
        }
    }


    /**
     * 根据id查询随访日记信息
     * @param id
     * @return
     */
    @RequestMapping("/findArrangeDiaryById")
    public Result findArrangeDiaryById(Integer id) {
        try {
            Map<String,Object> result = arrangeDiaryService.findArrangeDiaryById(id);
            return new Result(true,MessageConstant.GET_ARRANGE_DIARY_SUCCESS,result);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ARRANGE_DIARY_FAIL);
        }

    }


    /**
     * 根据id删除随访日记信息
     * @param id
     * @return
     */
    @RequestMapping("/deleteArrangeDiary")
    public Result deleteArrangeDiary(Integer id) {
        try {
            arrangeDiaryService.deleteArrangeDiary(id);
            return new Result(true,MessageConstant.DELETE_ARRANGE_DIARY_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_ARRANGE_DIARY_FAIL);
        }
    }

    /**
     * 新增随访日记信息
     * @param map
     * @return
     */
    @RequestMapping("/addarrangeDiary")
    public Result addarrangeDiary(@RequestBody Map<String,Object> map) {
        try {
            Map formData = (Map) map.get("formData");
            List list = (List) map.get("fileList");
            Map<String,Object> fileList = (Map<String, Object>) list.get(0);
            Result result = arrangeDiaryService.addarrangeDiary(formData, fileList);
            //将上传文件名保存到redis中
            jedisPool.getResource().sadd(RedisConstant.ARRANGE_EXCEL_DB_RESOURCES,(String) fileList.get("fileName"));
            return result;
        } catch(Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_ARRANGE_DIARY_FAIL);
        }

    }

    /**
     * 分页查询随访日记列表数据(模糊查询)
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = arrangeDiaryService.pageQuery(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
            return new Result(true,MessageConstant.GET_ARRANGE_DIARY_LIST_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ARRANGE_DIARY_LIST_FAIL);
        }
    }



    /**
     * 数据文件上传到七牛云
     * @param imgFile
     * @return
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile) {
        try {
            //得到文件原来的名称
            String originalFilename = imgFile.getOriginalFilename();
            //不用原本的文件名,用后面随机生成的字符串作为文件名,而需要自行补充一个后缀
            //从而我们应该自己得到原文件的后缀名
            //为了找到最后的后缀名(带.的) 拿到最后一个点的位置 进行截取
            int index = originalFilename.lastIndexOf(".");
            //截取到文件的后缀名
            String suffix = originalFilename.substring(index);
            //用UUID得到个随机的唯一的字符串 把它加上后缀  作为保存的图片名
            String fileName = UUID.randomUUID().toString() + suffix;
            //用七牛云的工具类进行上传 参数为 文件的字节数组  和 文件名
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
            //保存到七牛云以后 将文件名保存到 对应的redis的set集合中
            jedisPool.getResource().sadd(RedisConstant.ARRANGE_EXCEL_RESOURCES,fileName);
            //返回结果
            String url = "http://q1jygupxd.bkt.clouddn.com/" + fileName;
            ArrayList<Map> list = new ArrayList<Map>();
            HashMap<String, String> map = new HashMap<>();
            map.put("name",originalFilename);
            map.put("fileName",fileName);
            map.put("url",url);
            list.add(map);
            return new Result(true, MessageConstant.UPLOAD_SUCCESS, list);
        } catch (IOException e) {
            e.printStackTrace();
            //出错的话 就返回没有上传成功
            return  new Result(false,MessageConstant.UPLOAD_FAIL);
        }
    }
}
